function replaceClass(a, b, id){
    document.getElementById(id).classList.add(b);
    document.getElementById(id).classList.remove(a);
}
function switch_step(a, b){
    if ((a == 1) && (b == 2) && (document.getElementById('go_to_step2').value == 'continue'))
    {
        replaceClass('active', 'inactive', 'booking_step1');
        replaceClass('inactive', 'active', 'booking_step2');
        replaceClass('step_active', 'step_inactive', 'step_1');
        replaceClass('step_inactive', 'step_active', 'step_2');
    }
    else
    {
        alert("Please check the data again.");
    }
}
function checkStreetValidity(elem, id, idValidity){
    var ok = false;
    var text = elem.value;

    text = text.replace(/(^\s+|\s+$)/g, '');
    elem.value = text

    if (document.getElementById('termsAndConditions').checked == true) {
        document.getElementById('termsAndConditions').checked = false;
        document.getElementById('completeReservationButton').disabled = true;
    }

    if (elem.value.length == 0) {
        ok = true;
    }
    if (ok)
    {
        document.getElementById(id).innerHTML = "Please enter your street address.";
        elem.style.borderColor = "#f00";
        document.getElementById(idValidity).value = 0;
    }
    else
    {

        elem.style.borderColor = "#000";
        document.getElementById(id).innerHTML = "";
        document.getElementById(idValidity).value = 1;

    }
}
function checkNameValidity(elem, id, idValidity) {
    var ok = false;
    var fieldIsEmpty = false;
    var leters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
    var text = elem.value;

    /* Remove trailing and leading spaces */
    text = text.replace(/(^\s+|\s+$)/g, '');
    elem.value = text;

    for (i = 0; i < elem.value.length; i++)
    {
        if (leters.indexOf(text[i]) < 0)
        {
            ok = true;
        }
        //alert(i);
    }
    if (elem.value.length == 0) {
        ok = true;
        fieldIsEmpty = true;
    }

    if (ok)
    {
        if (fieldIsEmpty == true) {
            document.getElementById(id).innerHTML = "Please enter your name.";
        }
        else {
            document.getElementById(id).innerHTML = "You have entered an invalid name. The name can not contain numbers or special chars.";
        }
        elem.style.borderColor = "#f00";
        document.getElementById(idValidity).value = 0;
    } else {
        elem.style.borderColor = "#000";
        document.getElementById(id).innerHTML = "";
        document.getElementById(idValidity).value = 1;
    }
}
function checkPhoneValidity(elem, id, idValidity) {
    var ok = true,
            fieldIsEmpty = false,
            regex = /^\+(?:[0-9] ?){6,14}[0-9]$/,
            phone = elem.value,
            termsCond = document.getElementById('termsAndConditions'),
            reservation = document.getElementById('completeReservationButton'),
            errorField = document.getElementById(id),
            isError = document.getElementById(idValidity).value;

    if (phone.length <= 4 || phone.length >= 16) {
        ok = false;
        if (phone.length === 0) {
            fieldIsEmpty = true;
        }
    }

    if (regex.test(phone) && ok) { // Valid international phone number
        ok = true;
    } else { // Invalid international phone number
        ok = false;
    }

    if (!ok) {
        if (fieldIsEmpty) {
            errorField.innerHTML = "Please enter a phone number";
            elem.style.borderColor = "#f00";
            document.getElementById(idValidity).value = 0;
        } else {
            errorField.innerHTML = "You have entered an invalid phone number. The phone number should be between 5 and 14 digits and must start with a +.";
            elem.style.borderColor = "#f00";
            document.getElementById(idValidity).value = 0;
        }
    } else {
        elem.style.borderColor = "#000";
        errorField.innerHTML = "";
        document.getElementById(idValidity).value = 1;
    }
}
function checkEmailValidity(elem, id, idValidity) {
    var ok = true;
    var chars = "@.";
    var em = elem.value;

    /* Remove trailing and leading spaces */
    em = em.replace(/(^\s+|\s+$)/g, '');
    elem.value = em;

    var email;
    email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

    if (!em.match(email)) {
        ok = false;
    } else {
        or = true;
    }
    if (!ok) {
        document.getElementById(id).innerHTML = "You have entered an invalid email address.";
        elem.style.borderColor = "#f00";
        document.getElementById(idValidity).value = 0;
    } else {
        elem.style.borderColor = "#000";
        document.getElementById(id).innerHTML = "";
        document.getElementById(idValidity).value = 1;
    }
}
function emailMatch(em1, em2, ider, idValidity) {
//                text = em2.value;
//                
//                /* Remove trailing and leading spaces */
//                text = text.replace(/(^\s+|\s+$)/g,'');
//                em2.value = text

    var email;
    email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    if (!(document.getElementById(em1).value == document.getElementById(em2).value)) {
        document.getElementById(ider).innerHTML = 'Email addresses don\'t match!';
        document.getElementById(em1).style.borderColor = '#f00';
        document.getElementById(em2).style.borderColor = '#f00';
        document.getElementById(idValidity).value = 0;
    } else {
        document.getElementById(ider).innerHTML = "";
        document.getElementById(idValidity).value = 1;
        document.getElementById(em2).style.borderColor = '#000';
        if (document.getElementById(em1).value.match(email)) {
            document.getElementById(em1).style.borderColor = '#000';
        }
    }
}
function chechFileldsValidityStep1() {
    var ok = true;
    if (document.getElementById('contactDetails.emaiAddress.continue').value == 0)
        ok = false;
    if (document.getElementById('contactDetails.confirmEmailAddress.continue').value == 0)
        ok = false;
    if (document.getElementById('contactDetails.phoneNumber.continue').value == 0)
        ok = false;
    
    var i = 1, testArray1 = checkFields_firstname(), testArray2 = checkFields_lastname();
    for(i; i<=8; i++){
        if(testArray1[i] !== undefined){
            if(testArray1[i] == 0){
                ok = false;
            }
            if(testArray2[i] == 0){
                ok = false;
            }
        } else {
            break;
        }
    }

    jQuery('input').each(function(index) {
        if (jQuery(this).attr('value') == '0') {
            jQuery(this).parent().find('input').css({'border': '2px inset #f00'});
        }
    });

    if (ok == true) {
        jQuery('input').each(function(index) {
            jQuery(this).parent().find('input').css({'border': '2px inset #000'});
        });
        document.getElementById('go_to_step2').value = 'continue';
        document.getElementById('booking_page_step').value = 1;
    } else {
        document.getElementById('go_to_step2').value = 'stop';
        document.getElementById('booking_page_step').value = 0;
    }
    return ok;
    
}
function checkCreditCardNumber(elem, errorId, idValidity) {
    var ok = false;
    var digits = "0123456789";
    var text = elem.value;

    /* Remove trailing and leading spaces */
    text = text.replace(/(^\s+|\s+$)/g, '');
    elem.value = text


    if (document.getElementById('termsAndConditions').checked == true) {
        document.getElementById('termsAndConditions').checked = false;
        document.getElementById('completeReservationButton').disabled = true;
    }

    for (i = 0; i < elem.value.length; i++) {
        if (digits.indexOf(text[i]) < 0) {
            ok = true;
        }
    }
    if (text.length != 16) {
        ok = true;
    }

    if (ok == true) {
        document.getElementById('creditCardError').innerHTML = "You have entered an invalid credit card number";
        document.getElementById(idValidity).value = 0;
        elem.style.borderColor = '#f00';
    } else {
        document.getElementById('creditCardError').innerHTML = "";
        document.getElementById(idValidity).value = 1;
        elem.style.borderColor = '#000';
    }
}
function checkExpirationDateMonth(yearId, monthId, errorId, idValidity, idValidityYear, idValidityMonth) {
    var elMonth = document.getElementById(monthId);
    var selectedMonth = elMonth.options[elMonth.selectedIndex];
    var month, year;
    var d = new Date();
    var ok = true;
    var resetYear = false;

    window.MonthHasBeenSet = true;

    if (window.YearHasBeenSet == true) {
        year = parseInt(document.getElementById(yearId).value);
    } else {
        year = 0;
    }

    if (document.getElementById('termsAndConditions').checked == true) {
        document.getElementById('termsAndConditions').checked = false;
        document.getElementById('completeReservationButton').disabled = true;
    }

    month = parseInt(selectedMonth.value);

    if (isNaN(month)) {
        ok = false;
    } else {
        if (year == d.getFullYear()) {
            if (month > d.getMonth()) {
                if (window.YearHasBeenSet == true) {
                    resetYear = true;
                    console.log('entered if');
                }
                ok = true;
            } else {
                ok = false;
            }
        } else {
            ok = true;
        }
    }

    if (!ok && window.YearHasBeenSet == true) {
        document.getElementById(monthId).style.borderColor = '#f00';
        document.getElementById(idValidityMonth).value = 0;
        document.getElementById(errorId).innerHTML = "Invalid date.";

        var validity = parseInt(document.getElementById(idValidityYear).value);
        validity = validity & parseInt(document.getElementById(idValidityMonth).value);
        document.getElementById(idValidity).value = validity

    } else if (!ok) {
        document.getElementById(idValidityMonth).value = 0;

        var validity = parseInt(document.getElementById(idValidityYear).value);
        validity = validity & parseInt(document.getElementById(idValidityMonth).value);
        document.getElementById(idValidity).value = validity

    } else {
        document.getElementById(monthId).style.borderColor = '#000';
        document.getElementById(idValidityMonth).value = 1;
        document.getElementById(errorId).innerHTML = "";

        var validity = parseInt(document.getElementById(idValidityYear).value);
        validity = validity & parseInt(document.getElementById(idValidityMonth).value);
        document.getElementById(idValidity).value = validity

    }

    if (resetYear) {
        document.getElementById(yearId).style.borderColor = '#000';
        document.getElementById(idValidityYear).value = 1;
        document.getElementById(errorId).innerHTML = "";

        var validity = parseInt(document.getElementById(idValidityYear).value);
        validity = validity & parseInt(document.getElementById(idValidityMonth).value);
        document.getElementById(idValidity).value = validity
    }
}
function checkExpirationDate(yearobj, monthId, errorId, idValidity, idValidityYear, idValidityMonth) {
    var elMonth = document.getElementById(monthId);
    var selectedMonth = elMonth.options[elMonth.selectedIndex];
    var year;
    var d = new Date();
    var ok = true;
    var currentYear = d.getFullYear()

    window.YearHasBeenSet = true;

    year = yearobj.options[yearobj.selectedIndex].value;
    month = parseInt(selectedMonth.value);

    if (document.getElementById('termsAndConditions').checked == true) {
        document.getElementById('termsAndConditions').checked = false;
        document.getElementById('completeReservationButton').disabled = true;
    }
    console.log(d.getMonth() + 1 + " ---- " + month);


    if (year >= currentYear) {
        if (year == currentYear && window.MonthHasBeenSet && month < (d.getMonth() + 1)) {
            ok = false;
        } else if (year == currentYear && window.MonthHasBeenSet && month >= (d.getMonth() + 1)) {
            ok = true;
        } else if (year == currentYear) {
            ok = true
        } else if (year > currentYear) {
            ok = true
        }
    } else {
        ok = false;
    }

    if (!ok && window.MonthHasBeenSet == true) {
        yearobj.style.borderColor = "#f00";
        document.getElementById(idValidityYear).value = 0;
        document.getElementById(errorId).innerHTML = "Invalid date."

        var validity = parseInt(document.getElementById(idValidityYear).value);
        validity = validity & parseInt(document.getElementById(idValidityMonth).value);
        document.getElementById(idValidity).value = validity
    } else if (!ok) {
        document.getElementById(idValidityYear).value = 0;

        var validity = parseInt(document.getElementById(idValidityYear).value);
        validity = validity & parseInt(document.getElementById(idValidityMonth).value);
        document.getElementById(idValidity).value = validity

    } else {
        yearobj.style.borderColor = "#000";
        document.getElementById(idValidityYear).value = 1;
        document.getElementById(errorId).innerHTML = ""

        var validity = parseInt(document.getElementById(idValidityYear).value);
        validity = validity & parseInt(document.getElementById(idValidityMonth).value);
        document.getElementById(idValidity).value = validity
    }
}
function checkCardSecurityCode(elem, errorId, idValidity) {
    var text = elem.value;

    /* Remove trailing and leading spaces */
    text = text.replace(/(^\s+|\s+$)/g, '');
    elem.value = text

    var digits = '0123456789';
    var ok = true;
    if (document.getElementById('termsAndConditions').checked == true) {
        document.getElementById('termsAndConditions').checked = false;
        document.getElementById('completeReservationButton').disabled = true;
    }
    for (i = 0; i < text.length; i++) {
        if (!(digits.indexOf(text[i]) >= 0)) {
            ok = false;
        }
    }
    if (elem.value.length <3) {
        ok = false;
    }
    if (!ok) {
        document.getElementById(errorId).innerHTML = "Please enter your 3 digit credit card security code";
        elem.style.borderColor = "#f00";
        document.getElementById(idValidity).value = 0;
    } else {
        document.getElementById(idValidity).value = 1;
        document.getElementById(errorId).innerHTML = "";
        elem.style.borderColor = "#000";
    }
}
function checkCityValidity(elem, errorId, idValidity) {
    var text = elem.value;

    /* Remove trailing and leading spaces */
    text = text.replace(/(^\s+|\s+$)/g, '');
    elem.value = text

    var leters = /^[a-zA-Z0-9]+(?:[\s-][a-zA-Z]+)*$/;
    if (document.getElementById('termsAndConditions').checked == true) {
        document.getElementById('termsAndConditions').checked = false;
        document.getElementById('completeReservationButton').disabled = true;
    }
    if (text.match(leters)) {
        document.getElementById(idValidity).value = 1
        document.getElementById(errorId).innerHTML = "";
        elem.style.borderColor = "#000";
    } else {
        document.getElementById(idValidity).value = 0
        document.getElementById(errorId).innerHTML = "You have entered an invalid city name";
        elem.style.borderColor = "#f00";
    }
}
function checkZipCode(elem, errorId, idValidity) {
    var numbers = /^[0-9]+$/;
    var text = elem.value;

    /* Remove trailing and leading spaces */
    text = text.replace(/(^\s+|\s+$)/g, '');
    elem.value = text

    if (document.getElementById('termsAndConditions').checked == true) {
        document.getElementById('termsAndConditions').checked = false;
        document.getElementById('completeReservationButton').disabled = true;
    }
    if (document.getElementById('country_select').value == "US") {
        if (text.length == 5) {
            if (text.match(numbers)) {
                document.getElementById(errorId).innerHTML = "";
                document.getElementById(idValidity).value = 1;
                elem.style.borderColor = '#000';
            } else {
                document.getElementById(errorId).innerHTML = "You have entered an invalid US ZIP code";
                document.getElementById(idValidity).value = 0;
                elem.style.borderColor = '#f00';
            }
        } else {
            document.getElementById(errorId).innerHTML = "You have entered an invalid US ZIP code";
            document.getElementById(idValidity).value = 0;
            elem.style.borderColor = '#f00';
        }
    } else {
        if (text.match(numbers)) {
            document.getElementById(errorId).innerHTML = "";
            document.getElementById(idValidity).value = 1;
            elem.style.borderColor = '#000';
        } else {
            document.getElementById(errorId).innerHTML = "You have entered an invalid ZIP code";
            document.getElementById(idValidity).value = 0;
            elem.style.borderColor = '#f00';
        }
    }
}
function checkCountry(elem, stateFieldId, stateLabelId, stateIdValidity) {
    var country = elem.options[elem.selectedIndex].value;
    if (document.getElementById('termsAndConditions').checked == true) {
        document.getElementById('termsAndConditions').checked = false;
        document.getElementById('completeReservationButton').disabled = true;
    }
    if (country == "US") {
        document.getElementById(stateFieldId).classList.remove('inactive');
        document.getElementById(stateFieldId).classList.add('active');
        document.getElementById(stateLabelId).classList.remove('inactive');
        document.getElementById(stateLabelId).classList.add('active');
        /*document.getElementById(stateIdValidity).value = 0;*/
    } else {
        document.getElementById(stateFieldId).classList.remove('active');
        document.getElementById(stateFieldId).classList.add('inactive');
        document.getElementById(stateLabelId).classList.remove('active');
        document.getElementById(stateLabelId).classList.add('inactive');
        document.getElementById(stateIdValidity).value = 1;
    }
}
function checkState(elem, errorId, idValidity) {
    if (document.getElementById('termsAndConditions').checked == true) {
        document.getElementById('termsAndConditions').checked = false;
        document.getElementById('completeReservationButton').disabled = true;
    }
    if (elem.options[elem.selectedIndex].value == "") {
        document.getElementById(errorId).innerHTML = "Selecte a state!";
        document.getElementById(idValidity).value = 0;
        elem.borderColor = "#f00";
    } else {
        document.getElementById(errorId).innerHTML = "";
        document.getElementById(idValidity).value = 1;
        elem.borderColor = "#000";
    }
}
function checkForm(elem, completeRezButtonId) {
    var correct;
    correct = 1;
    correct *= parseInt(document.getElementById('creditCardInformation.cardType.continue').value);
    correct *= parseInt(document.getElementById('creditCardInformation.creditCardNumber.continue').value);
    correct *= parseInt(document.getElementById('creditCardInformation.cardHolderFirstName.continue').value);
    correct *= parseInt(document.getElementById('creditCardInformation.cardHolderLastName.continue').value);
    correct *= parseInt(document.getElementById('creditCardInformation.expirationDate.continue').value);
    correct *= parseInt(document.getElementById('creditCardInformation.securityCode.continue').value);
    correct *= parseInt(document.getElementById('billingAdress.city.continue').value);
    correct *= parseInt(document.getElementById('billingAdress.zipCode.continue').value);
    correct *= parseInt(document.getElementById('billingAdress.stateField.continue').value);
    correct *= parseInt(document.getElementById('creditCardInformation.cardType.continue').value);

    jQuery('input').each(function(index) {
        if (jQuery(this).attr('value') == '0') {
            jQuery(this).parent().find('input, select').css({'border': '2px inset #f00'});
        }
    });
    if (correct === 1) {
        if (elem.checked) {
            document.getElementById(completeRezButtonId).disabled = false;
        } else {
            document.getElementById(completeRezButtonId).disabled = true;
        }
    } else {
        document.getElementById(completeRezButtonId).disabled = true;
        alert("Please check the data again.");
        jQuery('#cancelationPolicy').next().removeAttr('checked');
    }
}
function checkCardType(elem, errorId, idValidity) {
    if (document.getElementById('termsAndConditions').checked == true) {
        document.getElementById('termsAndConditions').checked = false;
        document.getElementById('completeReservationButton').disabled = true;
    }

    if (elem.options[elem.selectedIndex].value == '') {
        document.getElementById(errorId).innerHTML = "Select Your Card Type";
        document.getElementById(idValidity).value = 0;
        elem.style.borderColor = "#f00";
    } else {
        document.getElementById(errorId).innerHTML = "";
        document.getElementById(idValidity).value = 1;
        elem.style.borderColor = "#000";
    }
}


var checkFields_firstname = function(){
    var i=1, result = new Array(); 
    for(i;i<=9;i++){
        result[i] = $("input[id^=guest.firstName"+i+".continue]").val();
    }; 
    return result; 
};
var checkFields_lastname = function(){
    var i=1, result = new Array(); 
    for(i;i<=9;i++){
        result[i] = $("input[id^=guest.lastName"+i+".continue]").val();
    }; 
    return result; 
};